package com.goddess.yeecolor.fragment.ExerciseAll;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.goddess.yeecolor.Modle.Person;
import com.goddess.yeecolor.Modle.xRefreshMethed;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.ExerciseItem2Activit;
import com.goddess.yeecolor.activity.ExerciseItemActivity;
import com.goddess.yeecolor.activity.Exerciseltem3Activity;
import com.goddess.yeecolor.adapter.ExerciseFragmentSingeAdapter;
import com.goddess.yeecolor.base.BaseFragment;
import com.goddess.yeecolor.interfaceAll.OnClickListener;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.LogUtils;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.ToastUtils;
import com.goddess.yeecolor.view.dialog_progress;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动单个fragment
 */
public class ExerciseFragmentSinge extends BaseFragment{
    private dialog_progress dialog_progress;
    private int pid;//标题id获取内容
    private String TAG_PULL="NORMAR";
    private int lomode;//更多
    private ExerciseFragmentSingeAdapter adapter;
    private View mView;
    private XRefreshView xRefreshView;
    private RecyclerView mRecyclerview;
    private LinearLayoutManager mLinearManager;
    private List<Person> personList = new ArrayList<Person>();
    private boolean is=true;
    private Bundle mBundle;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mView==null){
            mView=inflater.inflate(R.layout.fragment_exercisefragmentsinge,container,false);
            mBundle=getArguments();
            if (mBundle!=null){
                pid=mBundle.getInt("id");
            }
            initFindView();
            initData();
        }
        return mView;
    }

    @Override
    public void initOnClck() {

    }
    public ExerciseFragmentSinge(){}

    private void setProgress(){
        dialog_progress=new dialog_progress(getActivity());
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }

    public static ExerciseFragmentSinge newInstance(int id) {
        ExerciseFragmentSinge newFragment = new ExerciseFragmentSinge();
        Bundle bundle = new Bundle();
        bundle.putInt("id",id);
        newFragment.setArguments(bundle);
        return newFragment;
    }
    @Override
    public void initFindView() {
        xRefreshView= (XRefreshView) mView.findViewById(R.id.exercisefragmentsinge_swipe);
        mRecyclerview= (RecyclerView) mView.findViewById(R.id.exercisefragmentsinge_recyclerview);
        mRecyclerview.setHasFixedSize(true);

    }
    private int page=1;
    @Override
    public void initData() {
        setProgress();
        Map<Object,Object>map=new HashMap<>();
        map.put("unique", SharePreceUtils.getOther("user","unique"));
        map.put("typed",pid+"");
        map.put("page",page+"");

        OkttpUtils.getIntence().OkttpPost(MyUrl.url+MyUrl.activityGetlist,map,handler,1);
    }


    private String jsonString;
    private int code;
    private int endfull;//判断是否有无数据
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");
                JSONObject object=new JSONObject(jsonString);
                code=object.getInt("code");
                if (code==100){
//                    ToastUtils.toast(object.getString("msg"));
                    JSONArray result=object.getJSONArray("result");
                    JSONObject extendss=object.getJSONObject("extends");
                    endfull=extendss.getInt("endfull");
                    for (int i=0;i<result.length();i++){
                        JSONObject data=result.getJSONObject(i);
                        Person person = new Person();
                        person.setId(data.optInt("id"));

                        person.setTitle(data.optString("title"));
                        person.setThumb(MyUrl.imagurl+data.optString("thumb"));
                        person.setArea1(data.optInt("area1"));
                        person.setArea2(data.optInt("area2"));
                        person.setEvents_class(data.optInt("events_class"));
                        person.setHost_location(data.optString("host_location"));
                        person.setAdd_date(data.optString("add_date"));
                        person.setFee(data.optString("fee"));
                        person.setPrice(data.optString("price"));
                        person.setState(data.optInt("state"));
                        person.setInfo(data.optString("info"));
                        person.setStop(data.optInt("stop"));
                        if (TAG_PULL.equals("NORMAR")){
                            personList.add(person);
                        }
                        else if (TAG_PULL.equals("MODE")){
                            personList.add(person);
                            adapter.insert(person, adapter.getAdapterItemCount());
                        }
                        else {
                            adapter.insert(person, adapter.getAdapterItemCount());
                        }
                    }
                }else if (code==403){
                    JSONObject result=object.getJSONObject("result");
                    String msgss=result.getString("msg");
                    ToastUtils.toast(msgss);
                    if (msgss.equals("暂无数据！")){
                        xRefreshView.stopLoadMore(true);
                    }
                }
                if (TAG_PULL.equals("PULL")){
                    xRefreshView.stopRefresh(true);
                }else if (TAG_PULL.equals("MODE")){

                    xRefreshView.stopLoadMore(true);
                }
                else {
                    adapter = new ExerciseFragmentSingeAdapter(personList, getContext());
                    mLinearManager = new LinearLayoutManager(getContext());
                    mRecyclerview.setLayoutManager(mLinearManager);
                    mRecyclerview.setAdapter(adapter);
                    xRefreshMethed.setRefrsh(xRefreshView);
                    View view=LayoutInflater.from(getActivity()).inflate(R.layout.empty,null);
                    xRefreshView.setEmptyView(view);
                    adapter.setCustomLoadMoreView(new XRefreshViewFooter(getContext()));
                    /**
                     * 点击事件
                     */
                    adapter.setOnClickListener(new OnClickListener() {
                        @Override
                        public void OnClick(View view, int position) {

                            int p=personList.get(position).getState();
                            Bundle bundle=new Bundle();
                            bundle.putString("typed",pid+"");
                            bundle.putString("title",personList.get(position).getTitle());
                            bundle.putString("id",personList.get(position).getId()+"");
                            if (p==2){
                                JumpIntents.startIntent(getActivity(), ExerciseItem2Activit.class,bundle);
                            }
                            else if (p==1){
                                JumpIntents.startIntent(getActivity(), Exerciseltem3Activity.class,bundle);
                            }
                            else if (p==0){
                                JumpIntents.startIntent(getActivity(), ExerciseItemActivity.class,bundle);
                            }
                        }
                        @Override
                        public void OnLongClick(View view, int position) {

                        }
                    });
                    /**
                     * 上拉 和下拉
                     */
                    xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
                        @Override
                        public void onRefresh() {
                            super.onRefresh();
//                setProgress();
                            page=1;
                            TAG_PULL="PULL";
                            personList.clear();
                            if (adapter!=null){
                                adapter.clear();
                            }
                            adapter.notifyDataSetChanged();//防止数据不再顶部  刷新
                            initData();
                        }
                        @Override
                        public void onLoadMore(boolean isSilence) {
                            super.onLoadMore(isSilence);
//                setProgress();
                            TAG_PULL="MODE";
                            page++;
                            initData();
                        }
                    });
                }
                if (dialog_progress.isShowing()){
                    dialog_progress.dismiss();
                }
            }
            catch (Exception e){
            }
        }
    };
    @Override
    public void onStop() {
        super.onStop();
        if (xRefreshView!=null){
            xRefreshView.stopRefresh(true);
            xRefreshView.stopLoadMore(true);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dialog_progress!=null&&dialog_progress.isShowing()){
            dialog_progress.dismiss();
        }
    }
}
